/** *****  远程搜索的mixin  *******/

import { getBasicData } from '@/api/declare' // 客户数据字典
import { debounce, trim } from '@/libs/tools'
function queryBasicData (keyWord, dataMainType) {
    return getBasicData({
        keyWord,
        dataMainType,
        pageNum: 1,
        pageSize: 20
    })
}

/**
 * 处理基础参数下拉框的远程搜索
 * @param {String} keyword 搜索值
 * @param {Sting} dataMainType 基础类型
 * @param {String} listName 数据下拉列表
 * @param {String} loadingName 加载状态名
 * @param {Array} defListName 用户缓存的默认下拉列表名
 * @param {Function} mapFn map循环函数
 * @param {Function} cb 查询成功回调函数
 */
function handleMethod (
    keyword,
    dataMainType,
    listName,
    loadingName,
    defListName,
    mapFn,
    cb
) {
    const value = trim(keyword)
    const first = this[listName][0]
    if (keyword && first && (value === first.value || value === first.label)) return
    if (value || this[defListName] === undefined) {
        this[loadingName] = true
        queryBasicData(value, dataMainType)
            .then(res => {
                if (res.data.code === '0000') {
                    const list = res.data.data.list.map(mapFn)
                    if (!value) {
                        this[defListName] = list.slice()
                    }
                    this[listName] = list
                    cb && cb(list)
                }
                this[loadingName] = false
            })
            .catch(err => {
                this[loadingName] = false
            })
    } else {
        const list = this[defListName].slice()
        this[listName] = list
        cb && cb(list)
    }
}

// 监管方式列表
export const supvModecdSelectMx = {
    data () {
        return {
            supvModecdLoading: false,
            supvModecdList: []
        }
    },
    methods: {
        querySupvModecdList: debounce(function (key, cb) {
            this.searchSupvModecd(key, cb)
        }, 350),
        searchSupvModecd (key, cb) {
            handleMethod.call(
                this,
                key,
                'TRADE_MODE',
                'supvModecdList',
                'supvModecdLoading',
                'defSupvModecdList',
                function (item) {
                    return {
                        value: item.dataCode,
                        label: item.dataName
                    }
                },
                cb
            )
        }
    }
}

// 运输方式默认列表
export const trspModecdMx = {
    data () {
        return {
            trspModecdLoading: false,
            trspModecdList: []
        }
    },
    methods: {
        queryTrspModecdList: debounce(function (key, cb) {
            this.searchTrspModecd(key, cb)
        }, 350),
        searchTrspModecd (key, cb) {
            handleMethod.call(
                this,
                key,
                'TRAF_MODE',
                'trspModecdList',
                'trspModecdLoading',
                'defTrspModecdList',
                function (item) {
                    return {
                        value: item.dataCode,
                        label: item.dataName
                    }
                },
                cb
            )
        }
    }
}

// 进境管别列表
export const impexpPortcdMx = {
    data () {
        return {
            impexpPortcdLoading: false,
            impexpPortcdList: []
        }
    },
    methods: {
        queryImpexpPortcdList: debounce(function (key, cb) {
            this.searchImpexpPortcdList(key, cb)
        }, 350),
        searchImpexpPortcdList (key, cb) {
            handleMethod.call(
                this,
                key,
                'CUSTOMS_CODE',
                'impexpPortcdList',
                'impexpPortcdLoading',
                'defImpexpPortcdList',
                function (item) {
                    return {
                        value: item.dataCode,
                        label: item.dataName
                    }
                },
                cb
            )
        }
    }
}

// 主管海关列表
export const dclPlcCuscdMx = {
    data () {
        return {
            dclPlcCuscdLoading: false,
            dclPlcCuscdList: []
        }
    },
    methods: {
        queryDclPlcCuscdList: debounce(function (key, cb) {
            this.searchDclPlcCuscdList(key, cb)
        }, 350),
        searchDclPlcCuscdList (key, cb) {
            handleMethod.call(
                this,
                key,
                'CUSTOMS_CODE',
                'dclPlcCuscdList',
                'dclPlcCuscdLoading',
                'defDclPlcCuscdList',
                function (item) {
                    return {
                        value: item.dataCode,
                        label: item.dataName
                    }
                },
                cb
            )
        }
    }
}

// 起运/运抵国(地区)
export const stshipTrsarvNatcdMx = {
    data () {
        return {
            stshipTrsarvNatcdLoading: false,
            stshipTrsarvNatcdList: []
        }
    },
    methods: {
        queryStshipTrsarvNatcdList: debounce(function (key, cb) {
            this.searchStshipTrsarvNatcdList(key, cb)
        }, 350),
        searchStshipTrsarvNatcdList (key, cb) {
            handleMethod.call(
                this,
                key,
                'INVT_CONTY_REG',
                'stshipTrsarvNatcdList',
                'stshipTrsarvNatcdLoading',
                'defStshipTrsarvNatcdList',
                function (item) {
                    return {
                        value: item.dataSecondType,
                        label: item.dataName
                    }
                },
                cb
            )
        }
    }
}

// 最终目的国
export const destinationNatcdMx = {
    data () {
        return {
            destinationNatcdLoading: false,
            destinationNatcdList: []
        }
    },
    methods: {
        queryDestinationNatcdList: debounce(function (key, cb) {
            this.searchDestinationNatcdList(key, cb)
        }, 350),
        searchDestinationNatcdList (key, cb) {
            handleMethod.call(
                this,
                key,
                'INVT_CONTY_REG',
                'destinationNatcdList',
                'destinationNatcdLoading',
                'defDestinationNatcdList',
                function (item) {
                    return {
                        value: item.dataSecondType,
                        label: item.dataName
                    }
                },
                cb
            )
        }
    }
}

// 报关单类型
export const decTypeMx = {
    data () {
        return {
            decTypeLoading: false,
            decTypeList: []
        }
    },
    methods: {
        queryDecTypeList: debounce(function (key, cb) {
            this.searchDecTypeList(key, cb)
        }, 350),
        searchDecTypeList (key, cb) {
            handleMethod.call(
                this,
                key,
                'INVT_DECTYPE',
                'decTypeList',
                'decTypeLoading',
                'defDecTypeList',
                function (item) {
                    return {
                        value: item.dataCode,
                        label: item.dataName
                    }
                },
                cb
            )
        }
    }
}

// 币制
export const currcdMx = {
    data () {
        return {
            currcdLoading: false,
            currcdList: []
        }
    },
    methods: {
        queryCurrcdList: debounce(function (key, cb) {
            this.searchCurrcdList(key, cb)
        }, 350),
        searchCurrcdList (key, cb) {
            handleMethod.call(
                this,
                key,
                'CURRENCY_CODE',
                'currcdList',
                'currcdLoading',
                'defCurrcdList',
                function (item) {
                    return {
                        value: item.dataSecondType,
                        label: item.dataName
                    }
                },
                cb
            )
        }
    }
}
// 申报计量单位
export const unitcdMx = {
    data () {
        return {
            unitcdLoading: false,
            unitcdList: []
        }
    },
    methods: {
        queryUnitcdList: debounce(function (key, cb) {
            this.searchUnitcdList(key, cb)
        }, 350),
        searchUnitcdList (key, cb) {
            handleMethod.call(
                this,
                key,
                'UNIT',
                'unitcdList',
                'unitcdLoading',
                'DeflUnitcdList',
                function (item) {
                    return {
                        value: item.dataCode,
                        label: item.dataName
                    }
                },
                cb
            )
        }
    }
}

// 征免方式
export const lvyrlfModecdMx = {
    data () {
        return {
            lvyrlfModecdLoading: false,
            lvyrlfModecdList: []
        }
    },
    methods: {
        queryLvyrlfModecdList: debounce(function (key, cb) {
            this.searchLvyrlfModecdList(key, cb)
        }, 350),
        searchLvyrlfModecdList (key, cb) {
            handleMethod.call(
                this,
                key,
                'DUTY_MODE',
                'lvyrlfModecdList',
                'lvyrlfModecdLoading',
                'DefLvyrlfModecdList',
                function (item) {
                    return {
                        value: item.dataCode,
                        label: item.dataName
                    }
                },
                cb
            )
        }
    }
}
